You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

[customId].js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { Grid, Typography } from '@mui/material';
  2. import { Box, Container } from '@mui/system';
  3. import { dehydrate, QueryClient } from '@tanstack/react-query';
  4. import { useTranslation } from 'next-i18next';
  5. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  6. import Image from 'next/image';
  7. import { useRouter } from 'next/router';
  8. import React from 'react';
  9. import GridItem from '../../components/grid-item/GridItem';
  10. import Loader from '../../components/loader/Loader';
  11. import ProductCard from '../../components/product-card/ProductCard';
  12. import TabContent from '../../components/tab-content/TabContent';
  13. import { useFetchSingleProduct } from '../../hooks/useFetchProductData';
  14. import { getProductData } from '../../requests/products/producDataRequest';
  15. import { useStore, useStoreUpdate } from '../../store/cart-context';
  16. const SingleProduct = () => {
  17. const { t } = useTranslation('products');
  18. const { addCartValue } = useStoreUpdate();
  19. const { cartStorage } = useStore();
  20. const router = useRouter();
  21. const { customId } = router.query;
  22. const { data, isLoading } = useFetchSingleProduct(customId);
  23. const addProductToCart = (quantity) => addCartValue(data.product, quantity);
  24. const inCart = cartStorage?.some(
  25. (item) => item.product.customID === data?.product.customID
  26. )
  27. ? true
  28. : false;
  29. if (isLoading) {
  30. return <Loader loading={isLoading} />;
  31. }
  32. return (
  33. <Box
  34. sx={{
  35. display: 'flex',
  36. flexDirection: 'column',
  37. }}
  38. >
  39. <Container>
  40. <Typography
  41. fontFamily={'body1.fontFamily'}
  42. fontSize="32px"
  43. sx={{ mt: 25, height: '100%', color: 'primary.main' }}
  44. >
  45. {data.product.name}
  46. </Typography>
  47. <Grid container spacing={2}>
  48. <Grid sx={{ display: 'flex' }} item md={6} sm={12}>
  49. <Image
  50. src={data.product.image}
  51. alt="product"
  52. width={900}
  53. height={700}
  54. />
  55. </Grid>
  56. <TabContent
  57. description={data?.product.description}
  58. inCart={inCart}
  59. price={data?.product.price}
  60. category={data?.product.category}
  61. addProductToCart={addProductToCart}
  62. />
  63. </Grid>
  64. <Typography
  65. sx={{
  66. mt: { xs: '60px', md: '100px', lg: '150px' },
  67. mb: 5,
  68. color: 'primary.main',
  69. fontSize: '32px',
  70. }}
  71. >
  72. {t('products:similar')}
  73. </Typography>
  74. <Grid container spacing={2}>
  75. {data.similarProducts.map((product) => (
  76. <GridItem key={product._id}>
  77. <ProductCard product={product} />
  78. </GridItem>
  79. ))}
  80. </Grid>
  81. </Container>
  82. </Box>
  83. );
  84. };
  85. export const getServerSideProps = async (context) => {
  86. const { params } = context;
  87. const { customId } = params;
  88. const queryClient = new QueryClient();
  89. await queryClient.prefetchQuery(
  90. ['product', customId],
  91. async () => await getProductData(customId)
  92. );
  93. return {
  94. props: {
  95. dehydratatedState: dehydrate(queryClient),
  96. ...(await serverSideTranslations(context.locale, ['products'])),
  97. },
  98. };
  99. };
  100. export default SingleProduct;